from setting import *
from animation import *

class Supply(pygame.sprite.Sprite):
    def __init__(self):
        self._layer = 8
        self.groups = allgroup,supplygroup
        pygame.sprite.Sprite.__init__(self,self.groups)
        self.animation = Animation()
        self.images = self.animation.load_image('images/supply/bulletsupply.png',249,81,1,3)
        #必不可少的两条代码
        self.image = self.images[0]
        self.rect = self.image.get_rect()
        self.mask = pygame.mask.from_surface(self.image)
        #初设速度、角度
        self.x_speed = 2.00
        self.y_speed = 2.00
        self.speed = 2
        self.duration = 20000
        self.motion_type = 0
        self.start_time = pygame.time.get_ticks()
        # self.clock.get_timer()
     

    #会自动调用
    def update(self):
        current_time = pygame.time.get_ticks()
        if current_time - self.start_time > self.duration:
            self.kill()
        self.image = self.animation.get_current_image()
        if random.randint(0, 100) == 1:
            self.x_speed = random.randint(-2, 2)
            self.y_speed = random.randint(-2, 2)
        self.image = self.animation.get_current_image()
  
        #镜面反射移动,供给出现的位置可能为负数或者超过屏幕边界，因此要留有判断的余量，
        #不能直接 self.rect.x < 0
        if self.rect.x > SCENEWIDTH or self.rect.x < -50:
            self.x_speed = -self.x_speed
            # print(self.rect.x,self.rect.y)
        if self.rect.y  > SCENEHEIGHT  or self.rect.y < -50:
            self.y_speed = -self.y_speed
            # print(self.rect.x,self.rect.y)

        self.rect.x += self.x_speed
        self.rect.y += self.y_speed
        
      

    #供给默认从中心计算
    def set_pos(self, x, y):
        self.rect.center = (x,y)
        # self.rect.y = y

